package com.daiyilu.common;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;
//
//enum SmsChannel{
//    LUOSIMAO("The given name of me");
//    private String context;
//    private String getContext(){
//        return this.context;
//    }
//    private SmsChannel(String context){
//        this.context = context;
//    }
//}

public class SmsUtil {
    private static final String SMS_URL_LUOSIMAO = "https://sms-api.luosimao.com/v1/send.json";
    private static final String SMS_BANCH_URL_LUOSIMAO = "http://sms-api.luosimao.com/v1/send_batch.json";

//    public static void main(String[] args){
//        sendSmsByLuosimao("18571475594","验证码111111，您正登录仓海优选商城，如非本人操作请联系客服。【仓海优选】");
//    }

    /**
     * 通过【LUOSIMAO】短信网关发送短信
     * @param mobile 手机号码
     * @param content 短信内容
     */
    public static boolean sendSmsByLuosimao(String mobile, String content) {
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest.post(SMS_URL_LUOSIMAO).basicAuth("api", "key-550f70f2709415f605501beb7e6ff193")
                    .field("mobile", mobile)
                    .field("message", content)
                    .asJson();
            JSONObject jsonObj = jsonResponse.getBody().getObject();
            int error_code = jsonObj.getInt("error");
            String error_msg = jsonObj.getString("msg");
            if(error_code == 0){
                return true;
            } else {
                //TODO:有时间将短信模块单独作为微服务，并将错误数据记录入库
               // logger.error("发送短信失败,对方手机号码:[" + mobile + "],短信内容:[" + content + "],错误代码:[ " + error_code + "],错误信息:[" + error_msg + "]");
            }
        } catch (UnirestException e) {
            //logger.error("发送短信失败,对方手机号码:[" + mobile + "],短信内容:[" + content + "],错误信息:[ " + e.getLocalizedMessage() + "]");
        }

        return false;
    }
    /**
     * 通过【LUOSIMAO】短信批量网关发送短信
     * @param mobileList 手机号码
     * @param content 短信内容
     */
    public static boolean sendBanchSmsByLuosimao(String mobileList, String content) {
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest.post(SMS_BANCH_URL_LUOSIMAO).basicAuth("api", "key-550f70f2709415f605501beb7e6ff193")
                    .field("mobile_list", mobileList)
                    .field("message", content)
                    .asJson();
            JSONObject jsonObj = jsonResponse.getBody().getObject();
            int error_code = jsonObj.getInt("error");
            String error_msg = jsonObj.getString("msg");
            if(error_code == 0) {
                return true;
            } else {
//                //TODO:有时间将短信模块单独作为微服务，并将错误数据记录入库
//                logger.error("发送短信失败,对方手机号码:[" + mobileList + "],短信内容:[" + content + "],错误代码:[ " + error_code + "],错误信息:[" + error_msg + "]");
            }
        } catch (UnirestException e) {
//            logger.error("发送短信失败,对方手机号码:[" + mobileList + "],短信内容:[" + content + "],错误信息:[ " + e.getLocalizedMessage() + "]");
        }

        return false;
    }
}
